define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/customers/index' + location.search,
                    // add_url: 'customer/customers/add',
                    edit_url: 'customer/customers/edit',
                    // del_url: 'customer/customers/del',
                     multi_url: 'customer/customers/multi',
                    import_url: 'customer/customers/import',
                    table: 'customer_customers',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'external_userid', title: __('External_userid')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'type', title: __('Type')},
                        {field: 'gender', title: __('Gender')},
                        // {field: 'unionid', title: __('Unionid'), operate: 'LIKE'},
                        {field: 'corp_name', title: __('Corp_name'), operate: 'LIKE'},
                        // {field: 'user.username', title: __('User.username'), operate: 'LIKE'},
                        // {field: 'user.avatar', title: __('User.avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'customertype.name', title: __('Type.name'), operate: 'LIKE'},
                        {field: 'level.name', title: __('Level.name'), operate: 'LIKE'},
                        {field: 'schedule.name', title: __('Schedule.name'), operate: 'LIKE'},
                        // {field: 'corp_name', title: __('Corp_name'), operate: 'LIKE'},
                        // {field: 'corp_full_name', title: __('Corp_full_name'), operate: 'LIKE'},

                        // {field: 'follow_userid', title: __('Follow_userid'), operate: 'LIKE'},
                        {field: 'customer_type_id', title: __('Customer_type_id'), operate: 'LIKE'},
                        {field: 'customer_schedule_id', title: __('Customer_schedule_id'), operate: 'LIKE'},
                        {field: 'customer_level_id', title: __('Customer_level_id')},
                        {field: 'province_id', title: __('Province_id')},
                        {field: 'city_id', title: __('City_id')},
                        {field: 'area_id', title: __('Area_id')},
                        {field: 'yonghuo_time', title: __('Yonghuo_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime1},
                        {field: 'project', title: __('Project'), operate: 'LIKE'},
                        {field: 'contact_mobile', title: __('Contact_mobile'), operate: 'LIKE'},
                        {field: 'enemy', title: __('Enemy'), operate: 'LIKE'},
                        {field: 'success_lv', title: __('Success_lv'), operate: 'LIKE'},
                        // {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'customer/customers/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'customer/customers/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'customer/customers/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
